package com.lx.huimail.activity;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lx.huimail.R;
import com.lx.huimail.configs.Config;
import com.lx.huimail.utils.DateTimePickDialogUtil;
import com.lx.huimail.utils.TestDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class SearchActivity extends Activity implements View.OnClickListener{
    RelativeLayout fanhui;
 TextView shaixuan;
    String str;
    AlertDialog dialog;
    TextView tv1;
    TextView tv2;
    EditText danjuhao;
    Button ok;
    Button cancel;
    ListView lv;
    String settlement;
    List<Map<String, String>> list=new ArrayList<Map<String,String>>();
    PoiAdapter adapter;
    String start,end;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        settlement=getIntent().getStringExtra("settlement");
        initView();
        search();
        SimpleDateFormat formatter = new SimpleDateFormat    ("yyyy年MM月dd日 HH:mm ");
        Date curDate =new Date(System.currentTimeMillis());//获取当前时间
        str = formatter.format(curDate);
    }
    private  void initView(){
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        shaixuan=(TextView)findViewById(R.id.shaixuan);
        lv=(ListView)findViewById(R.id.lv);
        fanhui.setOnClickListener(this);
        shaixuan.setOnClickListener(this);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.fanhui:
                SearchActivity.this.finish();
             break;
            case R.id.shaixuan:

                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.dialog_select,
                        (ViewGroup)findViewById(R.id.dialog_select));
                AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
                builder .setView(layout);
                dialog=builder.create();
                dialog.show();
                tv1=(TextView)layout.findViewById(R.id.tv1);
                tv2=(TextView) layout.findViewById(R.id.tv2);
                danjuhao=(EditText)layout.findViewById(R.id.danjuhao);
                ok=(Button)layout.findViewById(R.id.ok);
                cancel=(Button)layout.findViewById(R.id.cancel);
              final   String initStartDateTime = str; // 初始化开始时间
              final   String initEndDateTime = str; // 初始化结束时间
                tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
//                                MoneyWaterActivity.this, initEndDateTime);
//                        dateTimePicKDialog.dateTimePicKDialog(tv1);
                        TestDialog dialog  = new TestDialog(SearchActivity.this, R.style.Dialog);
                        dialog.setTitle("选择时间");
                        dialog.setBackButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setConfirmButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tv1.setText(TestDialog.getDate());
                            }
                        });
                        dialog.show();

                    }
                });
                tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
//                                MoneyWaterActivity.this, initEndDateTime);
//                        dateTimePicKDialog.dateTimePicKDialog(tv2);
                        TestDialog dialog  = new TestDialog(SearchActivity.this, R.style.Dialog);
                        dialog.setTitle("选择时间");
                        dialog.setBackButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setConfirmButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                tv2.setText(TestDialog.getDate());
                            }
                        });

                        dialog.show();

                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap<String,String> map=new HashMap<String, String>();
                        map.put("danjuhaoEQ",danjuhao.getText().toString());
                        map.put("createtimeGE",tv1.getText().toString());
                        map.put("createtimeLE",tv2.getText().toString());
                        map.put("settlement_id",settlement);
                        Gson gson=new Gson();
                        String str=gson.toJson(map).toString();
                        OkHttpUtils
                                .post()
                                .url(Config.url+"report/searchalldaqudetail1.do")
                                .addParams("data", str)
                                .addParams("mobile", "true")
                                .addParams("page", "1")
                                .addParams("rows", "20")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {

                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                    System.out.print("筛选=="+response);
                                        try {
                                            JSONObject o1=new JSONObject(response);
                                            JSONArray a1=o1.getJSONArray("rows");
                                            List<Map<String, String>> lists=new ArrayList<Map<String,String>>();
                                            if (a1.length()>0) {
                                                for (int i = 0; i < a1.length(); i++) {
                                                    JSONObject a2 = a1.getJSONObject(i);
                                                    Map<String, String> map = new HashMap<String, String>();
                                                    map.put("custname", a2.getString("custname"));
                                                    map.put("danjuhao", a2.getString("danjuhao"));
                                                    map.put("qichu", a2.getString("qichu"));
                                                    map.put("jiecun", a2.getString("jiecun"));
                                                    map.put("inmoney", a2.getString("inmoney"));
                                                    map.put("outmoney", a2.getString("outmoney"));
                                                    map.put("createtime", a2.getString("createtime"));
                                                    lists.add(map);
                                                }
                                                PoiAdapter adapter = new PoiAdapter(SearchActivity.this, lists);
                                                lv.setAdapter(adapter);
                                            }else{
                                                lv.setVisibility(View.GONE);
                                            }
                                        }catch(JSONException e){
                                            e.printStackTrace();
                                        }
                                        dialog.dismiss();
                                    }

                                });
                    }
                });
                break;
        }
    }
    private void search(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("settlement_id",settlement);
        Gson gson=new Gson();
        String str=gson.toJson(map).toString();
        OkHttpUtils
                .post()
                .url(Config.url+"report/searchalldaqudetail1.do")
                .addParams("data", str)
                .addParams("mobile", "true")
                .addParams("page", "1")
                .addParams("rows", "20")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.print("预结算列表--"+response);
                        try {
                            JSONObject o1=new JSONObject(response);
                            JSONArray a1=o1.getJSONArray("rows");
                            for (int i=0;i<a1.length();i++){
                                JSONObject a2=a1.getJSONObject(i);
                                Map<String,String> map=new HashMap<String,String>();
                                map.put("custname",a2.getString("custname"));
                                map.put("danjuhao", a2.getString("danjuhao"));
                                map.put("qichu", a2.getString("qichu"));
                                map.put("jiecun", a2.getString("jiecun"));
                                map.put("inmoney", a2.getString("inmoney"));
                                map.put("outmoney", a2.getString("outmoney"));
                                map.put("createtime", a2.getString("createtime"));
                                list.add(map);
                            }
                           PoiAdapter adapter = new PoiAdapter(SearchActivity.this,list);
                            lv.setAdapter(adapter);
                        }catch(JSONException e){
                            e.printStackTrace();
                        }
                    }

                });
    }
    public class PoiAdapter extends BaseAdapter {
        private Context context;
        List<Map<String, String>> List;
        private LayoutInflater mInflater;
        public PoiAdapter(Context context,List<Map<String, String>> List){
            this.List = List;
            this.context = context;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return List.size();
        }
        @Override
        public Object getItem(int position) {
            return List.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_yujiesuan, null);
                holder.mingcheng = (TextView)convertView.findViewById(R.id.mingcheng);
                holder.danjuhao= (TextView)convertView.findViewById(R.id.danjuhao);
                holder.qichu = (TextView)convertView.findViewById(R.id.qichu);
                holder.jiecun = (TextView)convertView.findViewById(R.id.jiecun);
                holder.shou = (TextView)convertView.findViewById(R.id.shou);
                holder.chu= (TextView)convertView.findViewById(R.id.chu);
                holder.time= (TextView)convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.mingcheng.setText(List.get(position).get("custname"));
            holder.danjuhao.setText(List.get(position).get("danjuhao"));
            holder.qichu.setText(List.get(position).get("qichu"));
            holder.jiecun.setText(List.get(position).get("jiecun"));
            holder.shou.setText(List.get(position).get("inmoney"));
            holder.chu.setText(List.get(position).get("outmoney"));
            holder.time.setText(List.get(position).get("createtime"));

            return convertView;

        }
    }
    public final class ViewHolder{

        public TextView mingcheng;
        public TextView danjuhao;
        public TextView qichu;
        public TextView jiecun;
        public TextView shou;
        public TextView chu;
        public TextView time;

    }
    }
